using System;
using System.Drawing;
using Clandestine.Base;

namespace Clandestine.Graphics
{
	public class SubTexture : Texture
	{
		private Size size;
		public override Size Size { get { return size; } }
		private Texture parent;
		
		internal SubTexture (Texture parent, Rectangle subsection, string identifier, bool keepAlive = false)
			: base (parent.Graphics, identifier, parent.Interpolation, keepAlive)
		{
			this.TexturePointer = parent.TexturePointer;
			parent.ReferenceCount++;
			this.parent = parent;
			
			float x = subsection.X; // float, so everything is done w/float math!
			float y = subsection.Y;
			base.TopLeftTexCoord = new float[2] { x / parent.Size.Width, y / parent.Size.Height };
			base.BottomRightTexCoord = new float[2] { (x + subsection.Width) / parent.Size.Width, (y + subsection.Height) / parent.Size.Height };
			
			this.size = subsection.Size;
		}
		
		internal override void Destroy()
		{
			Log.i("Deleting SubTexture {0}", this.Identifier);
			base.Graphics.Textures.Remove(parent);
			// don't actually delete it, as it isn't ours... it's our parents.
			this.TexturePointer = 0;
		}
	}
}

